package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.Role;
import com.itheima.health.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CheckItemServiceImpl
 * @Description TODO
 * @Author ly
 * @Company 深圳黑马程序员
 * @Date 2019/9/11 9:55
 * @Version V1.0
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    @Override
    public List<Integer> selectPermissionIdsOfARole(int id) {

        List<Integer> list = roleDao.selectPermissionIdsOfARole(id);

        return list;

    }

    @Override
    public void add(Role role) {
        roleDao.add(role);
        Integer roleId = role.getId();
        List<Integer> permissionIds = role.getPermissionIds();
        List<Integer> menuIds = role.getMenuIds();
        setRoleAndMenu(roleId, menuIds);
        setRoleAndPermission(roleId, permissionIds);
    }

    @Override
    public void delete(Integer id) {
        // 1：判断中间表中是否存在数据，使用检查项id，查询中间表，获取数量
        long count = roleDao.findCountByRoleId(id);
        // 中间表有数据
        if(count>0){
            throw new RuntimeException("删除的检查项在检查组中存在引用关系，不能删除，请先删除中间表的数据");
        }
        roleDao.deleteById(id);
    }

    @Override
    public void update(Role role) {
        Integer roleId = role.getId();
        List<Integer> permissionIds = role.getPermissionIds();
        List<Integer> menuIds = role.getMenuIds();

        roleDao.deleteRoleAndPermissionByRoleId(roleId);
        roleDao.deleteRoleAndMenuByRoleId(roleId);

        roleDao.update(role);

        setRoleAndMenu(roleId, menuIds);
        setRoleAndPermission(roleId, permissionIds);
    }

    @Override
    public Role select(Integer id) {
        Role role = roleDao.findById(id);

        return role;
    }

    @Override
    public PageResult selectPage(QueryPageBean queryPageBean) {

        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        // 1：初始化
        PageHelper.startPage(currentPage,pageSize);
        // 2：定义条件查询
        List<Role> list = roleDao.findPageByCondition(queryString);
        // 3：处理成PageHelper支持的javabean
        PageInfo<Role> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public List<Role> selectAll() {
        List<Role> list = roleDao.findAll();

        return list;
    }

    // 表示向检查组和检查项的中间表中插入数据
    private void setRoleAndPermission(Integer roleId, List<Integer> permissionIds) {
        if(permissionIds!=null){

            for (Integer permissionId : permissionIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("permissionId",permissionId);
                roleDao.setRoleAndPermission(map);
            }

        }

    }

    // 表示向检查组和检查项的中间表中插入数据
    private void setRoleAndMenu(Integer roleId, List<Integer> menuIds) {
        if(menuIds!=null){

            for (Integer menuId : menuIds) {
                Map<String, Integer> map = new HashMap<>();
                map.put("roleId",roleId);
                map.put("menuIds",menuId);
                roleDao.setRoleAndMenu(map);
            }

        }

    }
}
